package com.android.waterdetection.fragment;

import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;

import com.android.waterdetection.R;
import com.android.waterdetection.bean.CommonBean;
import com.android.waterdetection.bean.TokenBean;
import com.android.waterdetection.utils.Constant;
import com.android.waterdetection.utils.OrderService;
import com.android.waterdetection.utils.ToastUtil;

import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.logging.HttpLoggingInterceptor;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava.RxJavaCallAdapterFactory;
import retrofit2.converter.gson.GsonConverterFactory;

public class OrderFragment extends Fragment {

    private static final String TAG = "OrderFragment";

    private Button mBtnMotorTurn;
    private Button mBtnMotorOff;

    private Retrofit mRetrofit;
    private OrderService mOrderService;
    private OkHttpClient.Builder mOkClient;

    public View onCreateView(@NonNull LayoutInflater inflater,
                             ViewGroup container, Bundle savedInstanceState) {
        View root = inflater.inflate(R.layout.fragment_order, container, false);
        initView(root);
        initData();
        initListener();
        return root;
    }

    private void initListener() {
        mBtnMotorTurn.setOnClickListener(v -> {
            mOrderService.createCommon(Constant.PROJECT_ID, Constant.PROJECT_ID,"{open : on}").enqueue(new Callback<CommonBean>() {
                @Override
                public void onResponse(Call<CommonBean> call, Response<CommonBean> response) {
                    Log.d(TAG, "createCommon->onResponse-message-->" + response.message());
                }

                @Override
                public void onFailure(Call<CommonBean> call, Throwable t) {
                    Log.d(TAG, "createCommon->onFailure-t-->" + t.getMessage());
                }
            });
            ToastUtil.showToast(requireContext(), "开启增氧成功");
        });

        mBtnMotorOff.setOnClickListener(v -> {
            mOrderService.createCommon(Constant.PROJECT_ID, Constant.PROJECT_ID,"{open : off}").enqueue(new Callback<CommonBean>() {
                @Override
                public void onResponse(Call<CommonBean> call, Response<CommonBean> response) {
                    Log.d(TAG, "createCommon->onResponse-message-->" + response.message());
                }

                @Override
                public void onFailure(Call<CommonBean> call, Throwable t) {
                    Log.d(TAG, "createCommon->onFailure-t-->" + t.getMessage());
                }
            });
            ToastUtil.showToast(requireContext(), "关闭增氧成功");
        });
    }

    private void initData() {
        //网络请求拦截器
        mOkClient = new OkHttpClient.Builder();
        mOkClient.addInterceptor(chain -> {
            Request request = chain.request();
            request.newBuilder()
                    .method(request.method(), request.body()).build();
            return chain.proceed(request);
        });

        HttpLoggingInterceptor httpLoggingInterceptor = new HttpLoggingInterceptor();
        httpLoggingInterceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
        mOkClient.addInterceptor(httpLoggingInterceptor);
        mRetrofit = new Retrofit.Builder()
                .baseUrl(Constant.IOT_BASE_URL)
                .addCallAdapterFactory(RxJavaCallAdapterFactory.create())
                .addConverterFactory(GsonConverterFactory.create())
                .client(mOkClient.build())
                .build();
        mOrderService = mRetrofit.create(OrderService.class);

        //获取token
        mOrderService.getToKen(Constant.APP_ID, Constant.SECRET).enqueue(new Callback<TokenBean>() {
            @Override
            public void onResponse(Call<TokenBean> call, Response<TokenBean> response) {
                Log.d(TAG, "getToKen->onResponse-isSuccessful-->" + response.isSuccessful());
                Log.d(TAG, "getToKen->onResponse-toString-->" + response.message());
            }

            @Override
            public void onFailure(Call<TokenBean> call, Throwable t) {
                Log.d(TAG, "getToKen->getMessage-t-->" + t.getMessage());
            }
        });
    }

    private void initView(View root) {
        mBtnMotorTurn = root.findViewById(R.id.btn_on);
        mBtnMotorOff = root.findViewById(R.id.btn_off);
    }
}